﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using Demo;
using Microsoft.Xna.Framework.Graphics;

namespace DeferredWaterDemo
{
    public class Camera : GameComponent
    { 
        float mYaw;
        float mPitch;
        float mSpeed;
        Vector3 mTranslation;
        Matrix mView;
        Matrix mProjection;

        public Matrix Projection
        {
            get
            {
                return mProjection;
            }
        }

        public Matrix View
        {
            get
            {
                return mView;
            }
        }

        public float Yaw
        {
            get
            {
                return mYaw;
            }
            set
            {
                mYaw = value;
                if (mYaw >= MathHelper.Pi * 2)
                    mYaw -= MathHelper.Pi * 2;
                else if (mYaw < 0)
                    mYaw += MathHelper.Pi * 2;
            }
        }

        public float Pitch
        {
            get
            {
                return mPitch;
            }
            set
            {
                mPitch = value;
                if (mPitch >= MathHelper.PiOver2)
                    mPitch = MathHelper.PiOver2;
                else if (mPitch < -MathHelper.PiOver2)
                    mPitch = -MathHelper.PiOver2;
            }
        }

        public Vector3 Position {
            set
            {
                mTranslation = -value;
            }
            get
            {
                return mTranslation;
            }
        }

        public Camera(Vector3 inPosition, float inYaw, float inPitch, float inSpeed)
            : base(Core.Demo as Game)
        {
            mView = Matrix.Identity;
            mTranslation = -inPosition;

            mYaw = inYaw;
            mPitch = inPitch;
            mSpeed = inSpeed;

            Viewport v = Core.Demo.GraphicsDevice.Viewport;
            mProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)v.Width / (float)v.Height, 5.0f, 1000.0f);
            Update(null);   //Creates the view matrix
        }

        public override void Update(GameTime gameTime)
        {
            Viewport v = Core.Demo.GraphicsDevice.Viewport;
            mProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)v.Width / (float)v.Height, 1.0f, 1000.0f);
            Vector3 d = Vector3.Zero;
            Matrix viewWorld = Matrix.Invert(mView);
            if(Core.Demo.IsKeyDown(Keys.W) || Core.Demo.LeftMouseDown)
                d += viewWorld.Forward;
            if (Core.Demo.IsKeyDown(Keys.S) || Core.Demo.RightMouseDown)
                d += viewWorld.Backward;
            if(Core.Demo.IsKeyDown(Keys.A))
                d += viewWorld.Left;
            if(Core.Demo.IsKeyDown(Keys.D))
                d += viewWorld.Right;
            mSpeed = .1f;
            mTranslation -= (d * mSpeed);
            mView = Matrix.CreateTranslation(mTranslation) * Matrix.CreateFromAxisAngle(Vector3.Up, -mYaw) * Matrix.CreateFromAxisAngle(Vector3.Right, -mPitch);
        }
    }
}
